package com.zhengxiang.onlinexamination.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhengxiang.onlinexamination.dao.QuBankMapper;
import com.zhengxiang.onlinexamination.dao.QuMapper;
import com.zhengxiang.onlinexamination.entity.Qu;
import com.zhengxiang.onlinexamination.entity.QuBank;
import com.zhengxiang.onlinexamination.entity.vo.QuBankVo;
import com.zhengxiang.onlinexamination.service.QuBankService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 题库(ZQuBank)表服务实现类
 *
 * @author 
 * @since 2023-03-10 10:56:10
 */
@Transactional(rollbackFor = Exception.class)
@Service("zQuBankService")
public class QuBankServiceImpl extends ServiceImpl<QuBankMapper, QuBank> implements QuBankService {

    @Resource
    private QuBankMapper quBankMapper;

    @Resource
    private QuMapper quMapper;

    @Override
    public List<QuBank> queryAll(QuBankVo quBankVo) {
        return quBankMapper.selectList(null);
    }

    @Override
    public IPage<QuBank> queryQuBankAll(QuBankVo zQuBankTo) {
        QueryWrapper<QuBank> q = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(zQuBankTo.getQuBankName())){
            q.lambda().like(QuBank::getQuBankName,zQuBankTo.getQuBankName());
        }
        if(Objects.nonNull(zQuBankTo.getQuBankId())){
            q.lambda().eq(QuBank::getQuBankId,zQuBankTo.getQuBankId());
        }
        return quBankMapper.selectPage(new Page<>(zQuBankTo.getCurrent(),zQuBankTo.getSize()),q);
    }


    @Override
    public void saveQuBank(QuBankVo zQuBankTo) {
        quBankMapper.insert(zQuBankTo);
    }

    @Override
    public void delQuBank(QuBankVo zQuBankTo) {
        UpdateWrapper<QuBank> u = new UpdateWrapper<>();
        u.lambda().in(QuBank::getQuBankId,zQuBankTo.getDelBankIds());
        quBankMapper.delete(u);
    }

    @Override
    public List<Qu> queryBankDetail(QuBankVo quBankVo) {
        QueryWrapper<Qu> q = new QueryWrapper<>();
        q.lambda().eq(Qu::getQuBankId,quBankVo.getQuBankId());
       return quMapper.queryAll(new Page<Qu>(1,-1),q).getRecords();
    }
}

